package com.lagou.edu;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient//开启注册中心 客户端 通用型注解 比如注册到Eureka Nacos等 说明从SpringCloud 的 Edgware 版本 开始  不加注解 也ok 建议加上
@EnableCircuitBreaker // 开启熔断
/**
 * 注解简化写法
 * @SpringCloudApplication =
 @SpringBootApplication+@EnableDiscoveryClient+@EnableCircuitBrea
 ker
 */
public class AutodeliverApplication {
    public static
    void main(String[] args) {
        SpringApplication.run(AutodeliverApplication.class, args);
    }

    /**
     * 注⼊RestTemplate
     *
     * @return
     */
    @Bean
    @LoadBalanced// Ribbon负载均衡
    public
    RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
    /**
     * 在被监控的微服务中注册一个serlvet，后期我们就是通过访问这个servlet来获取该服务的Hystrix监控数据的
     * 前提：被监控的微服务需要引入springboot的actuator功能
     * @return
     */
    @Bean
    public
    ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
       /* registrationBean.addUrlMappings("/actuator/turbine.stream");*/
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}